
//               File: InvoiceDataAdapter
//        Description: Invoice
//             Author: DeKlarit - Language C# - Generator Version 10_1_3-31907 - Specifier Version 10_1_3.31907
//       Program type: Deklarit Business Component
//          Main DBMS: Microsoft SqlServer

namespace DeklaritModel {
   using System;
   using System.Collections;
   using Deklarit.Utils;
   using System.Data;
   using System.Data.SqlClient;
   using System.Data.Common;
   using Deklarit.Data;
   using System.ComponentModel.Design;
   using System.Runtime.Serialization;
   using System.Security.Permissions;
   using System.Data.SqlTypes;
   public partial class InvoiceDataAdapter : System.Data.IDataAdapter, IInvoiceDataAdapter
   {
      private InvoiceDataSet InvoiceSet ;
      private IDbTransaction daCurrentTransaction ;
      private InvoiceDataSet.InvoiceLineRow rowInvoiceLine ;
      private InvoiceDataSet.InvoiceRow rowInvoice ;
      public InvoiceDataAdapter( )
      {
      }

      private void InitializeMembers( )
      {
         InvoiceSet = new InvoiceDataSet() ;
         this.Initialize( ) ;
         dsDefault = new DataStore( new SqlServer2005Handler( ), "System.Data.SqlClient", DeklaritModel.Configuration.ConnectionString, DeklaritTransaction.TransactionSlotName) ;
      }

      ///  <summary>
      ///   Gets or sets the Transaction object to be used  by  InvoiceDataAdapterclasses to
      ///    commit changes to the Database.
      ///  </summary>
      public IDbTransaction Transaction
      {
         get {
            return daCurrentTransaction ;
         }

         set {
            daCurrentTransaction = value ;
         }

      }

      ///  <summary>
      ///   Gets or sets the missing mapping action.
      ///  </summary>
      public MissingMappingAction MissingMappingAction
      {
         get {
            return System.Data.MissingMappingAction.Passthrough ;
         }

         set {
         }

      }

      ///  <summary>
      ///   Gets or sets the missing schema action.
      ///  </summary>
      public MissingSchemaAction MissingSchemaAction
      {
         get {
            return System.Data.MissingSchemaAction.Ignore ;
         }

         set {
         }

      }

      ///  <summary>
      ///   Gets the table mappings.
      ///  </summary>
      public ITableMappingCollection TableMappings
      {
         get {
            System.Data.Common.DataTableMappingCollection tableMaps ;
            tableMaps = new System.Data.Common.DataTableMappingCollection() ;
            return tableMaps ;
         }

      }

      ///  <summary>
      ///   Fills the schema for the datasource.
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="schemaType">Schema type</param>
      public virtual DataTable[] FillSchema( DataSet dataSet ,
                                             SchemaType schemaType )
      {
         DataTable[] dataTables ;
         dataTables = new DataTable[dataSet.Tables.Count]  ;
         dataSet.Tables.CopyTo( dataTables, dataSet.Tables.Count) ;
         return dataTables ;
      }


      ///  <summary>
      ///   (2) Fills the specified Dataset with the record matching InvoiceId field(s) of the
      ///    given IDataRecord
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="dataRecord">Data Record</param>
      public virtual int Fill( InvoiceDataSet dataSet ,
                               IDataRecord dataRecord )
      {
         return Fill(dataSet, ((short)(dataRecord["InvoiceId"]))) ;
      }

      ///  <summary>
      ///   (3) Fills the specified Dataset with the record matching InvoiceId field(s) of the
      ///    given DataRow
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="dataRecord">Data Row</param>
      public virtual int Fill( InvoiceDataSet dataSet ,
                               DataRow dataRecord )
      {
         return Fill(dataSet, ((short)(dataRecord["InvoiceId"]))) ;
      }

      ///  <summary>
      ///   (4) Fills the specified Dataset with the record matching InvoiceId, if the record
      ///    is not found , throws an exception.
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="invoiceId">Invoice Id</param>
      public virtual int Fill( InvoiceDataSet dataSet ,
                               short invoiceId )
      {
         if ( ! FillByInvoiceId( dataSet, invoiceId) )
         {
            throw new InvoiceNotFoundException( string.Format(resourceManager.GetString("GXM_inex"), new   object[]  {"Invoice"})) ;
         }
         return 0 ;
      }


      ///  <summary>
      ///   (5) Fills the specified Dataset with the record matching InvoiceId in the fillDataParameters
      ///    array, if the array is null, it fills the Dataset loading all records in the table.
      ///
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      public virtual int Fill( InvoiceDataSet dataSet )
      {
         if ( fillDataParameters != null )
         {
            Fill( dataSet, System.Int16.Parse( fillDataParameters[0].Value.ToString())) ;
         }
         else
         {
            try
            {
               InitializeMembers( ) ;
               connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
               InvoiceSet = dataSet ;
               LoadChildInvoice( 0, -1) ;
               dataSet.AcceptChanges( ) ;
            }
            finally
            {
               this.Cleanup();
            }
         }
         return 0 ;
      }


      ///  <summary>
      ///   (6) Fills the specified dataset filtered by InvoiceIdfield(s).
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="invoiceId">Invoice Id</param>
      public virtual bool FillByInvoiceId( InvoiceDataSet dataSet ,
                                           short invoiceId )
      {
         InitializeMembers( ) ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         InvoiceSet = dataSet ;
         rowInvoice = InvoiceSet.Invoice.NewInvoiceRow() ;
         rowInvoice.InvoiceId = invoiceId ;
         try
         {
            LoadByInvoiceId( 0, -1) ;
            dataSet.AcceptChanges( ) ;
         }
         finally
         {
            this.Cleanup();
         }
         if ( RcdFound2 == 0 )
         {
            return false ;
         }
         return true ;
      }


      ///  <summary>
      ///   (6) Fills the specified dataset filtered by ClienteIdfield(s).
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="clienteId">Cliente</param>
      public virtual int FillByClienteId( InvoiceDataSet dataSet ,
                                          short clienteId )
      {
         InitializeMembers( ) ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         InvoiceSet = dataSet ;
         rowInvoice = InvoiceSet.Invoice.NewInvoiceRow() ;
         rowInvoice.ClienteId = clienteId ;
         try
         {
            LoadByClienteId( 0, -1) ;
            dataSet.AcceptChanges( ) ;
         }
         finally
         {
            this.Cleanup();
         }
         return 0 ;
      }


      ///  <summary>
      ///   (6) Fills a page of up to maxRows rows into the specified dataset filtered by ClienteIdfield(s)starting
      ///    at startRow
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="clienteId">Cliente</param>
      ///  <param name="startRow">Starting row</param>
      ///  <param name="maxRows">Max number of rows to load</param>
      public virtual int FillPageByClienteId( InvoiceDataSet dataSet ,
                                              short clienteId ,
                                              int startRow ,
                                              int maxRows )
      {
         InitializeMembers( ) ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         InvoiceSet = dataSet ;
         rowInvoice = InvoiceSet.Invoice.NewInvoiceRow() ;
         rowInvoice.ClienteId = clienteId ;
         try
         {
            LoadByClienteId( startRow, maxRows) ;
            dataSet.AcceptChanges( ) ;
         }
         finally
         {
            this.Cleanup();
         }
         return 0 ;
      }


      ///  <summary>
      ///   (9) Gets the record count filtered by ClienteIdfield(s).
      ///  </summary>
      ///  <param name="clienteId">Cliente</param>
      public virtual int GetRecordCountByClienteId( short clienteId )
      {
         int m_Count ;
         try
         {
            InitializeMembers( ) ;
            m_Count = GetInternalRecordCountByClienteId( clienteId ) ;
         }
         finally
         {
            this.Cleanup();
         }
         return m_Count ;
      }

      private int GetInternalRecordCountByClienteId( short clienteId )
      {
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         cmINVOICESELECT1 = connDefault.GetCommand("SELECT COUNT(*) FROM [Invoice] WITH (NOLOCK) WHERE [ClienteId] = @ClienteId ", false) ;
         if ( cmINVOICESELECT1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICESELECT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmINVOICESELECT1.SetParameter(0, clienteId);
         INVOICESELECT1 = cmINVOICESELECT1.FetchData() ;
         recordCount = ( ( INVOICESELECT1.IsDBNull(0) )  ? (int)(0) : INVOICESELECT1.GetInt32(0) ) ;
         INVOICESELECT1.Close();
         return recordCount ;
      }

      ///  <summary>
      ///   (8) Fills the specified dataset with the record matching InvoiceId in the the fillDataParameters
      ///    array, if the array is null, it fills the Dataset loading all records in the table.
      ///
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      public virtual int Fill( DataSet dataSet )
      {
         InvoiceSet = dataSet as InvoiceDataSet ;
         if ( InvoiceSet != null )
         {
            return Fill( InvoiceSet) ;
         }
         else
         {
            InvoiceSet = new InvoiceDataSet() ;
            Fill( InvoiceSet) ;
            dataSet.Merge( InvoiceSet) ;
            return 0 ;
         }
      }

      ///  <summary>
      ///   (11) Fills a page of up to maxRows rows into the specified dataset starting at startRow
      ///
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      ///  <param name="startRow">Starting row</param>
      ///  <param name="maxRows">Max number of rows to load</param>
      public virtual int FillPage( InvoiceDataSet dataSet ,
                                   int startRow ,
                                   int maxRows )
      {
         InitializeMembers( ) ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         InvoiceSet = dataSet ;
         try
         {
            LoadChildInvoice( startRow, maxRows) ;
            dataSet.AcceptChanges( ) ;
         }
         finally
         {
            this.Cleanup();
         }
         return 0 ;
      }


      ///  <summary>
      ///   (9) Gets the total record count.
      ///  </summary>
      public virtual int GetRecordCount( )
      {
         int m_Count ;
         try
         {
            InitializeMembers( ) ;
            m_Count = GetInternalRecordCount(  ) ;
         }
         finally
         {
            this.Cleanup();
         }
         return m_Count ;
      }

      private int GetInternalRecordCount( )
      {
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         cmINVOICESELECT2 = connDefault.GetCommand("SELECT COUNT(*) FROM [Invoice] WITH (NOLOCK) ", false) ;
         INVOICESELECT2 = cmINVOICESELECT2.FetchData() ;
         recordCount = ( ( INVOICESELECT2.IsDBNull(0) )  ? (int)(0) : INVOICESELECT2.GetInt32(0) ) ;
         INVOICESELECT2.Close();
         return recordCount ;
      }

      ///  <summary>
      ///   Gets the fill parameters array.
      ///  </summary>
      public virtual IDataParameter[] GetFillParameters( )
      {
         if ( fillDataParameters == null )
         {
            DbProviderFactory factory = System.Data.Common.DbProviderFactories.GetFactory( "System.Data.SqlClient") ;
            DbParameter parm0InvoiceId ;
            parm0InvoiceId = factory.CreateParameter() ;
            parm0InvoiceId.ParameterName = "InvoiceId" ;
            parm0InvoiceId.DbType = System.Data.DbType.Int16 ;
            fillDataParameters = new  DbParameter[]  {parm0InvoiceId} ;
         }
         return fillDataParameters ;
      }

      private DbParameter[] fillDataParameters ;

      ///  <summary>
      ///   Inserts, Updates or Deletes records from the specified InvoiceDataSet object.
      ///  </summary>
      ///  <param name="dataSet">DataSet</param>
      public virtual int Update( DataSet dataSet )
      {
         InitializeMembers( ) ;
         InvoiceSet = dataSet as InvoiceDataSet ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         if ( InvoiceSet != null )
         {
            try
            {
               connDefault.BeginTransaction();
               foreach( InvoiceDataSet.InvoiceRow row in InvoiceSet.Invoice )
               {
                  rowInvoice = row ;
                  if ( Deklarit.Data.Helpers.IsRowChanged(rowInvoice) )
                  {
                     ReadRowInvoice( ) ;
                     if ( rowInvoice.RowState == System.Data.DataRowState.Added )
                     {
                        InsertInvoice( ) ;
                     }
                     else
                     {
                        if ( _Gxremove )
                        {
                           Delete( ) ;
                        }
                        else
                        {
                           UpdateInvoice( ) ;
                        }
                     }
                  }
               }

               dataSet.AcceptChanges( ) ;
               connDefault.Commit();
            }
            catch ( Exception  )
            {
               connDefault.Rollback();
               throw  ;
            }
            finally
            {
               this.Cleanup();
            }
         }
         else
         {
            throw new System.ArgumentException( string.Format(resourceManager.GetString("GXM_nulldset"), new   object[]  {})) ;
         }
         return 0 ;
      }

      private void LoadRowInvoiceline( )
      {
         AddRowInvoiceline( ) ;
      }

      private void ReadRowInvoiceline( )
      {
         Gx_mode = Deklarit.Utils.Mode.FromRowState(rowInvoiceLine.RowState) ;
         if ( rowInvoiceLine.RowState != System.Data.DataRowState.Added )
         {
            m__InvoiceLineProductValueOriginal = rowInvoiceLine["InvoiceLineProductValue", System.Data.DataRowVersion.Original] ;
            m__InvoiceLineProductQuantityOriginal = rowInvoiceLine["InvoiceLineProductQuantity", System.Data.DataRowVersion.Original] ;
            m__ProductIdOriginal = rowInvoiceLine["ProductId", System.Data.DataRowVersion.Original] ;
         }
         else
         {
            m__InvoiceLineProductValueOriginal = rowInvoiceLine["InvoiceLineProductValue"] ;
            m__InvoiceLineProductQuantityOriginal = rowInvoiceLine["InvoiceLineProductQuantity"] ;
            m__ProductIdOriginal = rowInvoiceLine["ProductId"] ;
         }
         _Gxremove = (bool)((rowInvoiceLine.RowState==System.Data.DataRowState.Deleted)) ;
         if ( _Gxremove )
         {
            rowInvoiceLine = ((InvoiceDataSet.InvoiceLineRow)(Deklarit.Utils.DataSetUtil.CloneOriginalDataRow(rowInvoiceLine)));
         }
      }

      private void CheckExtendedTableInvoiceline( )
      {
          IDataReader PRODUCTSELECT1 ;
          ReadWriteCommand cmPRODUCTSELECT1 ;
         cmPRODUCTSELECT1 = connDefault.GetCommand("SELECT [ProductName] FROM [Product] WITH (NOLOCK) WHERE [ProductId] = @ProductId ", false) ;
         if ( cmPRODUCTSELECT1.IDbCommand.Parameters.Count == 0 )
         {
            cmPRODUCTSELECT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ProductId", System.Data.DbType.Int16));
         }
         cmPRODUCTSELECT1.SetParameter(0, rowInvoiceLine["ProductId"]);
         PRODUCTSELECT1 = cmPRODUCTSELECT1.FetchData() ;
         if ( !cmPRODUCTSELECT1.HasMoreRows )
         {
            PRODUCTSELECT1.Close();
            throw new ProductForeignKeyNotFoundException( string.Format(resourceManager.GetString("GXM_inex"), new   object[]  {"Product"})) ;
         }
         rowInvoiceLine["ProductName"] = dsDefault.Db.GetString(PRODUCTSELECT1, 0) ;
         PRODUCTSELECT1.Close();
      }

      private void CheckOptimisticConcurrencyInvoiceline( )
      {
          ReadWriteCommand cmINVOICELINESELECT1 ;
         if ( Gx_mode != Mode.Insert )
         {
            cmINVOICELINESELECT1 = connDefault.GetCommand("SELECT [InvoiceId], [InvoiceLineId], [InvoiceLineProductValue], [InvoiceLineProductQuantity], [ProductId] FROM [InvoiceLine] WITH (UPDLOCK) WHERE [InvoiceId] = @InvoiceId AND [InvoiceLineId] = @InvoiceLineId ", false) ;
            if ( cmINVOICELINESELECT1.IDbCommand.Parameters.Count == 0 )
            {
               cmINVOICELINESELECT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
               cmINVOICELINESELECT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineId", System.Data.DbType.Int16));
            }
            cmINVOICELINESELECT1.SetParameter(0, rowInvoiceLine["InvoiceId"]);
            cmINVOICELINESELECT1.SetParameter(1, rowInvoiceLine["InvoiceLineId"]);
            INVOICELINESELECT1 = cmINVOICELINESELECT1.FetchData() ;
            if ( cmINVOICELINESELECT1.Locked )
            {
               INVOICELINESELECT1.Close();
               throw new InvoiceLineDataLockedException( string.Format(resourceManager.GetString("GXM_lock"), new   object[]  {"InvoiceLine"})) ;
            }
            if ( !cmINVOICELINESELECT1.HasMoreRows || ( ! m__InvoiceLineProductValueOriginal.Equals(dsDefault.Db.GetDecimal(INVOICELINESELECT1, 2)) ) || ( ! m__InvoiceLineProductQuantityOriginal.Equals(dsDefault.Db.GetInt16(INVOICELINESELECT1, 3)) ) || ( ! m__ProductIdOriginal.Equals(dsDefault.Db.GetInt16(INVOICELINESELECT1, 4)) ) )
            {
               INVOICELINESELECT1.Close();
               throw new InvoiceLineDataChangedException( string.Format(resourceManager.GetString("GXM_waschg"), new   object[]  {"InvoiceLine"})) ;
            }
            INVOICELINESELECT1.Close();
         }
      }

      private void InsertInvoiceline( )
      {
          ReadWriteCommand cmINVOICELINEINSERT1 ;
         CheckExtendedTableInvoiceline( ) ;
         CheckOptimisticConcurrencyInvoiceline( ) ;
         AfterConfirmInvoiceline( ) ;
         cmINVOICELINEINSERT1 = connDefault.GetCommand("INSERT INTO [InvoiceLine] ([InvoiceId], [InvoiceLineId], [InvoiceLineProductValue], [InvoiceLineProductQuantity], [ProductId]) VALUES (@InvoiceId, @InvoiceLineId, @InvoiceLineProductValue, @InvoiceLineProductQuantity, @ProductId)", false) ;
         if ( cmINVOICELINEINSERT1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICELINEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
            cmINVOICELINEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineId", System.Data.DbType.Int16));
            cmINVOICELINEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineProductValue", System.Data.DbType.Currency));
            cmINVOICELINEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineProductQuantity", System.Data.DbType.Int16));
            cmINVOICELINEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ProductId", System.Data.DbType.Int16));
         }
         cmINVOICELINEINSERT1.ErrorMask = cmINVOICELINEINSERT1.ErrorMask  |  ErrorMask.DuplicateKeyError;
         // Using cursor INVOICELINEINSERT1
         cmINVOICELINEINSERT1.SetParameter(0, rowInvoiceLine["InvoiceId"]);
         cmINVOICELINEINSERT1.SetParameter(1, rowInvoiceLine["InvoiceLineId"]);
         cmINVOICELINEINSERT1.SetParameter(2, rowInvoiceLine["InvoiceLineProductValue"]);
         cmINVOICELINEINSERT1.SetParameter(3, rowInvoiceLine["InvoiceLineProductQuantity"]);
         cmINVOICELINEINSERT1.SetParameter(4, rowInvoiceLine["ProductId"]);
         cmINVOICELINEINSERT1.ExecuteStmt();
         if ( cmINVOICELINEINSERT1.DupKey )
         {
            throw new InvoiceLineDuplicateKeyException( string.Format(resourceManager.GetString("GXM_noupdate"), new   object[]  {})) ;
         }
         // Start of After( Insert) rules
         // End of After( Insert) rules
         OnInvoiceLineUpdated( new InvoiceLineEventArgs( rowInvoiceLine, Mode.Insert)) ;
         // Save values for previous() function.
      }

      private void UpdateInvoiceline( )
      {
          ReadWriteCommand cmINVOICELINEUPDATE1 ;
         CheckExtendedTableInvoiceline( ) ;
         CheckOptimisticConcurrencyInvoiceline( ) ;
         AfterConfirmInvoiceline( ) ;
         cmINVOICELINEUPDATE1 = connDefault.GetCommand("UPDATE [InvoiceLine] SET [InvoiceLineProductValue]=@InvoiceLineProductValue, [InvoiceLineProductQuantity]=@InvoiceLineProductQuantity, [ProductId]=@ProductId  WHERE [InvoiceId] = @InvoiceId AND [InvoiceLineId] = @InvoiceLineId", false) ;
         if ( cmINVOICELINEUPDATE1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICELINEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineProductValue", System.Data.DbType.Currency));
            cmINVOICELINEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineProductQuantity", System.Data.DbType.Int16));
            cmINVOICELINEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ProductId", System.Data.DbType.Int16));
            cmINVOICELINEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
            cmINVOICELINEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineId", System.Data.DbType.Int16));
         }
         // Using cursor INVOICELINEUPDATE1
         cmINVOICELINEUPDATE1.SetParameter(0, rowInvoiceLine["InvoiceLineProductValue"]);
         cmINVOICELINEUPDATE1.SetParameter(1, rowInvoiceLine["InvoiceLineProductQuantity"]);
         cmINVOICELINEUPDATE1.SetParameter(2, rowInvoiceLine["ProductId"]);
         cmINVOICELINEUPDATE1.SetParameter(3, rowInvoiceLine["InvoiceId"]);
         cmINVOICELINEUPDATE1.SetParameter(4, rowInvoiceLine["InvoiceLineId"]);
         cmINVOICELINEUPDATE1.ExecuteStmt();
         if ( cmINVOICELINEUPDATE1.Locked )
         {
            INVOICELINESELECT1.Close();
            throw new InvoiceLineDataLockedException( string.Format(resourceManager.GetString("GXM_lock"), new   object[]  {"InvoiceLine"})) ;
         }
         // Start of After( update) rules
         // End of After( update) rules
         OnInvoiceLineUpdated( new InvoiceLineEventArgs( rowInvoiceLine, Mode.Update)) ;
      }

      private void DeleteInvoiceline( )
      {
          ReadWriteCommand cmINVOICELINEDELETE1 ;
         Gx_mode = Mode.Delete ;
         CheckOptimisticConcurrencyInvoiceline( ) ;
         OnDeleteControlsInvoiceline( ) ;
         AfterConfirmInvoiceline( ) ;
         // No cascading delete specified.
         cmINVOICELINEDELETE1 = connDefault.GetCommand("DELETE FROM [InvoiceLine]  WHERE [InvoiceId] = @InvoiceId AND [InvoiceLineId] = @InvoiceLineId", false) ;
         if ( cmINVOICELINEDELETE1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICELINEDELETE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
            cmINVOICELINEDELETE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineId", System.Data.DbType.Int16));
         }
         cmINVOICELINEDELETE1.ErrorMask = cmINVOICELINEDELETE1.ErrorMask  |  ErrorMask.ForeignKeyError;
         // Using cursor INVOICELINEDELETE1
         cmINVOICELINEDELETE1.SetParameter(0, rowInvoiceLine["InvoiceId"]);
         cmINVOICELINEDELETE1.SetParameter(1, rowInvoiceLine["InvoiceLineId"]);
         cmINVOICELINEDELETE1.ExecuteStmt();
         // Start of After( delete) rules
         // End of After( delete) rules
         OnInvoiceLineUpdated( new InvoiceLineEventArgs( rowInvoiceLine, Mode.Delete)) ;
         rowInvoiceLine.Delete( ) ;
         sMode3 = Gx_mode ;
         Gx_mode = Mode.Delete ;
         Gx_mode = sMode3 ;
      }

      private void OnDeleteControlsInvoiceline( )
      {
          IDataReader PRODUCTSELECT2 ;
          ReadWriteCommand cmPRODUCTSELECT2 ;
         // Delete mode formulas
         cmPRODUCTSELECT2 = connDefault.GetCommand("SELECT [ProductName] FROM [Product] WITH (NOLOCK) WHERE [ProductId] = @ProductId ", false) ;
         if ( cmPRODUCTSELECT2.IDbCommand.Parameters.Count == 0 )
         {
            cmPRODUCTSELECT2.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ProductId", System.Data.DbType.Int16));
         }
         cmPRODUCTSELECT2.SetParameter(0, rowInvoiceLine["ProductId"]);
         PRODUCTSELECT2 = cmPRODUCTSELECT2.FetchData() ;
         if ( cmPRODUCTSELECT2.HasMoreRows )
         {
            rowInvoiceLine["ProductName"] = dsDefault.Db.GetString(PRODUCTSELECT2, 0) ;
         }
         PRODUCTSELECT2.Close();
      }

      private void ScanKeyStartInvoiceline( )
      {
         cmINVOICELINESELECT2 = connDefault.GetCommand("SELECT T1.[InvoiceId], T1.[InvoiceLineId], T2.[ProductName], T1.[InvoiceLineProductValue], T1.[InvoiceLineProductQuantity], T1.[ProductId] FROM ([InvoiceLine] T1 WITH (NOLOCK) INNER JOIN [Product] T2 WITH (NOLOCK) ON T2.[ProductId] = T1.[ProductId]) WHERE T1.[InvoiceId] = @InvoiceId ORDER BY T1.[InvoiceId], T1.[InvoiceLineId] ", false) ;
         if ( cmINVOICELINESELECT2.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICELINESELECT2.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         cmINVOICELINESELECT2.SetParameter(0, rowInvoiceLine["InvoiceId"]);
         INVOICELINESELECT2 = cmINVOICELINESELECT2.FetchData() ;
         RcdFound3 = 0 ;
         ScanKeyLoadInvoiceline( ) ;
         // Load Subordinate Levels
      }

      private void SubLvlScanStartInvoiceline( string sCondition ,
                                               int startRow ,
                                               int maxRows )
      {
         m_WhereStringSub = "" ;
         if ( maxRows >= 0 )
         {
            if ( startRow == 0 )
            {
               m_WhereString = sCondition ;
               m_SubSelTopString3 = "(SELECT TOP " + maxRows.ToString() + " TM1.[InvoiceId]  FROM [Invoice]  TM1 " + m_WhereString + " ORDER BY TM1.[InvoiceId] )" ;
               scmdbuf = "SELECT T1.[InvoiceId], T1.[InvoiceLineId], T3.[ProductName], T1.[InvoiceLineProductValue], T1.[InvoiceLineProductQuantity], T1.[ProductId] FROM (([InvoiceLine] T1 WITH (NOLOCK) INNER JOIN  " + m_SubSelTopString3 + "  TMX ON TMX.[InvoiceId] = T1.[InvoiceId]) INNER JOIN [Product] T3 WITH (NOLOCK) ON T3.[ProductId] = T1.[ProductId]) ORDER BY T1.[InvoiceId], T1.[InvoiceLineId]" ;
            }
            else
            {
               m_WhereString = sCondition ;
               m_SubSelTopString3 = "( SELECT * FROM ( SELECT TM1.[InvoiceId], ROW_NUMBER() OVER  (  ORDER BY TM1.[InvoiceId]  ) AS DK_PAGENUM   FROM [Invoice]  TM1  " + m_WhereString + " ) AS DK_PAGE WHERE DK_PAGENUM BETWEEN " + (startRow + 1).ToString() + " AND " + (startRow + maxRows).ToString() + ")" ;
               scmdbuf = "SELECT T1.[InvoiceId], T1.[InvoiceLineId], T3.[ProductName], T1.[InvoiceLineProductValue], T1.[InvoiceLineProductQuantity], T1.[ProductId] FROM (([InvoiceLine] T1 WITH (NOLOCK) INNER JOIN  " + m_SubSelTopString3 + "  TMX ON TMX.[InvoiceId] = T1.[InvoiceId]) INNER JOIN [Product] T3 WITH (NOLOCK) ON T3.[ProductId] = T1.[ProductId]) ORDER BY T1.[InvoiceId], T1.[InvoiceLineId]" ;
            }
         }
         else
         {
            m_WhereString = sCondition ;
            m_SubSelTopString3 = "[Invoice]" ;
            scmdbuf = "SELECT T1.[InvoiceId], T1.[InvoiceLineId], T3.[ProductName], T1.[InvoiceLineProductValue], T1.[InvoiceLineProductQuantity], T1.[ProductId] FROM (([InvoiceLine] T1 WITH (NOLOCK) INNER JOIN  " + m_SubSelTopString3 + "  TM1 WITH (NOLOCK) ON TM1.[InvoiceId] = T1.[InvoiceId]) INNER JOIN [Product] T3 WITH (NOLOCK) ON T3.[ProductId] = T1.[ProductId])" + m_WhereString + " ORDER BY T1.[InvoiceId], T1.[InvoiceLineId] " ;
         }
         cmINVOICELINESELECT2 = connDefault.GetCommand(scmdbuf, false) ;
      }

      private void SubLvlFetchInvoiceline( )
      {
         CreateNewRowInvoiceline( ) ;
         INVOICELINESELECT2 = cmINVOICELINESELECT2.FetchData() ;
         RcdFound3 = 0 ;
         ScanKeyLoadInvoiceline( ) ;
      }

      private void ScanKeyNextInvoiceline( )
      {
         cmINVOICELINESELECT2.HasMoreRows = INVOICELINESELECT2.Read() ;
         RcdFound3 = 0 ;
         ScanKeyLoadInvoiceline( ) ;
      }

      private void SkipNextInvoiceline( )
      {
         cmINVOICELINESELECT2.HasMoreRows = INVOICELINESELECT2.Read() ;
         RcdFound3 = 0 ;
         if ( cmINVOICELINESELECT2.HasMoreRows )
         {
            RcdFound3 = 1 ;
         }
      }

      private void ScanKeyLoadInvoiceline( )
      {
         sMode3 = Gx_mode ;
         Gx_mode = Mode.Display ;
         if ( cmINVOICELINESELECT2.HasMoreRows )
         {
            RcdFound3 = 1 ;
            rowInvoiceLine["InvoiceId"] = dsDefault.Db.GetInt16(INVOICELINESELECT2, 0) ;
            rowInvoiceLine["InvoiceLineId"] = dsDefault.Db.GetInt16(INVOICELINESELECT2, 1) ;
            rowInvoiceLine["ProductName"] = dsDefault.Db.GetString(INVOICELINESELECT2, 2) ;
            rowInvoiceLine["InvoiceLineProductValue"] = dsDefault.Db.GetDecimal(INVOICELINESELECT2, 3) ;
            rowInvoiceLine["InvoiceLineProductQuantity"] = dsDefault.Db.GetInt16(INVOICELINESELECT2, 4) ;
            rowInvoiceLine["ProductId"] = dsDefault.Db.GetInt16(INVOICELINESELECT2, 5) ;
            rowInvoiceLine["ProductName"] = dsDefault.Db.GetString(INVOICELINESELECT2, 2) ;
         }
         Gx_mode = sMode3 ;
      }

      private void ScanKeyEndInvoiceline( )
      {
         INVOICELINESELECT2.Close();
      }

      private void AfterConfirmInvoiceline( )
      {
         // After Confirm Rules
         OnInvoiceLineUpdating( new InvoiceLineEventArgs( rowInvoiceLine, Gx_mode)) ;
      }

      private void AddRowInvoiceline( )
      {
         InvoiceSet.InvoiceLine.AddInvoiceLineRow( rowInvoiceLine) ;
      }


      private void LoadRowInvoice( )
      {
         AddRowInvoice( ) ;
      }

      private void ReadRowInvoice( )
      {
         Gx_mode = Deklarit.Utils.Mode.FromRowState(rowInvoice.RowState) ;
         if ( rowInvoice.RowState != System.Data.DataRowState.Added )
         {
            m__InvoiceDescriptionOriginal = rowInvoice["InvoiceDescription", System.Data.DataRowVersion.Original] ;
            m__InvoiceLineLastIndexOriginal = rowInvoice["InvoiceLineLastIndex", System.Data.DataRowVersion.Original] ;
            m__ClienteIdOriginal = rowInvoice["ClienteId", System.Data.DataRowVersion.Original] ;
         }
         else
         {
            m__InvoiceDescriptionOriginal = rowInvoice["InvoiceDescription"] ;
            m__InvoiceLineLastIndexOriginal = rowInvoice["InvoiceLineLastIndex"] ;
            m__ClienteIdOriginal = rowInvoice["ClienteId"] ;
         }
         _Gxremove = (bool)((rowInvoice.RowState==System.Data.DataRowState.Deleted)) ;
         if ( _Gxremove )
         {
            rowInvoice = ((InvoiceDataSet.InvoiceRow)(Deklarit.Utils.DataSetUtil.CloneOriginalDataRow(rowInvoice)));
         }
      }

      private void LoadInvoice( )
      {
          IDataReader CLIENTESELECT1 ;
          ReadWriteCommand cmCLIENTESELECT1 ;
         cmCLIENTESELECT1 = connDefault.GetCommand("SELECT [ClienteName] FROM [Cliente] WITH (NOLOCK) WHERE [ClienteId] = @ClienteId ", false) ;
         if ( cmCLIENTESELECT1.IDbCommand.Parameters.Count == 0 )
         {
            cmCLIENTESELECT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmCLIENTESELECT1.SetParameter(0, rowInvoice["ClienteId"]);
         CLIENTESELECT1 = cmCLIENTESELECT1.FetchData() ;
         if ( cmCLIENTESELECT1.HasMoreRows )
         {
            rowInvoice["ClienteName"] = dsDefault.Db.GetString(CLIENTESELECT1, 0) ;
         }
         CLIENTESELECT1.Close();
      }

      private void CheckExtendedTableInvoice( )
      {
          IDataReader CLIENTESELECT2 ;
          ReadWriteCommand cmCLIENTESELECT2 ;
         cmCLIENTESELECT2 = connDefault.GetCommand("SELECT [ClienteName] FROM [Cliente] WITH (NOLOCK) WHERE [ClienteId] = @ClienteId ", false) ;
         if ( cmCLIENTESELECT2.IDbCommand.Parameters.Count == 0 )
         {
            cmCLIENTESELECT2.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmCLIENTESELECT2.SetParameter(0, rowInvoice["ClienteId"]);
         CLIENTESELECT2 = cmCLIENTESELECT2.FetchData() ;
         if ( !cmCLIENTESELECT2.HasMoreRows )
         {
            CLIENTESELECT2.Close();
            throw new ClienteForeignKeyNotFoundException( string.Format(resourceManager.GetString("GXM_inex"), new   object[]  {"Cliente"})) ;
         }
         rowInvoice["ClienteName"] = dsDefault.Db.GetString(CLIENTESELECT2, 0) ;
         CLIENTESELECT2.Close();
      }

      private void GetByPrimaryKey( )
      {
          IDataReader INVOICESELECT3 ;
          ReadWriteCommand cmINVOICESELECT3 ;
         cmINVOICESELECT3 = connDefault.GetCommand("SELECT [InvoiceId], [InvoiceDescription], [InvoiceLineLastIndex], [ClienteId] FROM [Invoice] WITH (NOLOCK) WHERE [InvoiceId] = @InvoiceId ", false) ;
         if ( cmINVOICESELECT3.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICESELECT3.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         cmINVOICESELECT3.SetParameter(0, rowInvoice["InvoiceId"]);
         INVOICESELECT3 = cmINVOICESELECT3.FetchData() ;
         if ( cmINVOICESELECT3.HasMoreRows )
         {
            RcdFound2 = 1 ;
            rowInvoice["InvoiceId"] = dsDefault.Db.GetInt16(INVOICESELECT3, 0) ;
            rowInvoice["InvoiceDescription"] = dsDefault.Db.GetString(INVOICESELECT3, 1) ;
            rowInvoice["InvoiceLineLastIndex"] = dsDefault.Db.GetInt16(INVOICESELECT3, 2) ;
            rowInvoice["ClienteId"] = dsDefault.Db.GetInt16(INVOICESELECT3, 3) ;
            sMode2 = Gx_mode ;
            Gx_mode = Mode.Display ;
            LoadInvoice( ) ;
            Gx_mode = sMode2 ;
         }
         else
         {
            RcdFound2 = 0 ;
         }
         INVOICESELECT3.Close();
      }

      private void CheckOptimisticConcurrencyInvoice( )
      {
          ReadWriteCommand cmINVOICESELECT4 ;
         if ( Gx_mode != Mode.Insert )
         {
            cmINVOICESELECT4 = connDefault.GetCommand("SELECT [InvoiceId], [InvoiceDescription], [InvoiceLineLastIndex], [ClienteId] FROM [Invoice] WITH (UPDLOCK) WHERE [InvoiceId] = @InvoiceId ", false) ;
            if ( cmINVOICESELECT4.IDbCommand.Parameters.Count == 0 )
            {
               cmINVOICESELECT4.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
            }
            cmINVOICESELECT4.SetParameter(0, rowInvoice["InvoiceId"]);
            INVOICESELECT4 = cmINVOICESELECT4.FetchData() ;
            if ( cmINVOICESELECT4.Locked )
            {
               INVOICESELECT4.Close();
               throw new InvoiceDataLockedException( string.Format(resourceManager.GetString("GXM_lock"), new   object[]  {"Invoice"})) ;
            }
            if ( !cmINVOICESELECT4.HasMoreRows || ( ! StringUtil.ObjectStringEquals(m__InvoiceDescriptionOriginal,dsDefault.Db.GetString(INVOICESELECT4, 1)) ) || ( ! m__InvoiceLineLastIndexOriginal.Equals(dsDefault.Db.GetInt16(INVOICESELECT4, 2)) ) || ( ! m__ClienteIdOriginal.Equals(dsDefault.Db.GetInt16(INVOICESELECT4, 3)) ) )
            {
               INVOICESELECT4.Close();
               throw new InvoiceDataChangedException( string.Format(resourceManager.GetString("GXM_waschg"), new   object[]  {"Invoice"})) ;
            }
            INVOICESELECT4.Close();
         }
      }

      private void InsertInvoice( )
      {
          IDataReader INVOICEINSERT1 ;
          ReadWriteCommand cmINVOICEINSERT1 ;
         CheckExtendedTableInvoice( ) ;
         CheckOptimisticConcurrencyInvoice( ) ;
         AfterConfirmInvoice( ) ;
         cmINVOICEINSERT1 = connDefault.GetCommand("INSERT INTO [Invoice] ([InvoiceDescription], [InvoiceLineLastIndex], [ClienteId]) VALUES (@InvoiceDescription, @InvoiceLineLastIndex, @ClienteId); SELECT SCOPE_IDENTITY()", false) ;
         if ( cmINVOICEINSERT1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceDescription", System.Data.DbType.AnsiStringFixedLength,20));
            cmINVOICEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineLastIndex", System.Data.DbType.Int16));
            cmINVOICEINSERT1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmINVOICEINSERT1.ErrorMask = cmINVOICEINSERT1.ErrorMask  |  ErrorMask.DuplicateKeyError;
         cmINVOICEINSERT1.SetParameter(0, rowInvoice["InvoiceDescription"]);
         cmINVOICEINSERT1.SetParameter(1, rowInvoice["InvoiceLineLastIndex"]);
         cmINVOICEINSERT1.SetParameter(2, rowInvoice["ClienteId"]);
         INVOICEINSERT1 = cmINVOICEINSERT1.FetchData() ;
         rowInvoice.InvoiceId = (short)(INVOICEINSERT1.GetDecimal(0)) ;
         INVOICEINSERT1.Close();
         // Start of After( Insert) rules
         // End of After( Insert) rules
         OnInvoiceUpdated( new InvoiceEventArgs( rowInvoice, Mode.Insert)) ;
         ProcessLevelInvoice( ) ;
         // Save values for previous() function.
         EndLevelInvoice( ) ;
      }

      private void UpdateInvoice( )
      {
          ReadWriteCommand cmINVOICEUPDATE1 ;
         CheckExtendedTableInvoice( ) ;
         CheckOptimisticConcurrencyInvoice( ) ;
         AfterConfirmInvoice( ) ;
         cmINVOICEUPDATE1 = connDefault.GetCommand("UPDATE [Invoice] SET [InvoiceDescription]=@InvoiceDescription, [InvoiceLineLastIndex]=@InvoiceLineLastIndex, [ClienteId]=@ClienteId  WHERE [InvoiceId] = @InvoiceId", false) ;
         if ( cmINVOICEUPDATE1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceDescription", System.Data.DbType.AnsiStringFixedLength,20));
            cmINVOICEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceLineLastIndex", System.Data.DbType.Int16));
            cmINVOICEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
            cmINVOICEUPDATE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         // Using cursor INVOICEUPDATE1
         cmINVOICEUPDATE1.SetParameter(0, rowInvoice["InvoiceDescription"]);
         cmINVOICEUPDATE1.SetParameter(1, rowInvoice["InvoiceLineLastIndex"]);
         cmINVOICEUPDATE1.SetParameter(2, rowInvoice["ClienteId"]);
         cmINVOICEUPDATE1.SetParameter(3, rowInvoice["InvoiceId"]);
         cmINVOICEUPDATE1.ExecuteStmt();
         if ( cmINVOICEUPDATE1.Locked )
         {
            INVOICESELECT4.Close();
            throw new InvoiceDataLockedException( string.Format(resourceManager.GetString("GXM_lock"), new   object[]  {"Invoice"})) ;
         }
         // Start of After( update) rules
         // End of After( update) rules
         OnInvoiceUpdated( new InvoiceEventArgs( rowInvoice, Mode.Update)) ;
         ProcessLevelInvoice( ) ;
         EndLevelInvoice( ) ;
      }

      private void Delete( )
      {
          ReadWriteCommand cmINVOICEDELETE1 ;
         Gx_mode = Mode.Delete ;
         CheckOptimisticConcurrencyInvoice( ) ;
         OnDeleteControlsInvoice( ) ;
         AfterConfirmInvoice( ) ;
         ProcessNestedLevelInvoiceline( ) ;
         cmINVOICEDELETE1 = connDefault.GetCommand("DELETE FROM [Invoice]  WHERE [InvoiceId] = @InvoiceId", false) ;
         if ( cmINVOICEDELETE1.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICEDELETE1.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         cmINVOICEDELETE1.ErrorMask = cmINVOICEDELETE1.ErrorMask  |  ErrorMask.ForeignKeyError;
         // Using cursor INVOICEDELETE1
         cmINVOICEDELETE1.SetParameter(0, rowInvoice["InvoiceId"]);
         cmINVOICEDELETE1.ExecuteStmt();
         // Start of After( delete) rules
         // End of After( delete) rules
         OnInvoiceUpdated( new InvoiceEventArgs( rowInvoice, Mode.Delete)) ;
         rowInvoice.Delete( ) ;
         sMode2 = Gx_mode ;
         Gx_mode = Mode.Delete ;
         EndLevelInvoice( ) ;
         Gx_mode = sMode2 ;
      }

      private void OnDeleteControlsInvoice( )
      {
          IDataReader CLIENTESELECT3 ;
          ReadWriteCommand cmCLIENTESELECT3 ;
         // Delete mode formulas
         cmCLIENTESELECT3 = connDefault.GetCommand("SELECT [ClienteName] FROM [Cliente] WITH (NOLOCK) WHERE [ClienteId] = @ClienteId ", false) ;
         if ( cmCLIENTESELECT3.IDbCommand.Parameters.Count == 0 )
         {
            cmCLIENTESELECT3.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmCLIENTESELECT3.SetParameter(0, rowInvoice["ClienteId"]);
         CLIENTESELECT3 = cmCLIENTESELECT3.FetchData() ;
         if ( cmCLIENTESELECT3.HasMoreRows )
         {
            rowInvoice["ClienteName"] = dsDefault.Db.GetString(CLIENTESELECT3, 0) ;
         }
         CLIENTESELECT3.Close();
      }

      private void ProcessNestedLevelInvoiceline( )
      {
         foreach( DataRow row in InvoiceSet.InvoiceLine )
         {
            rowInvoiceLine = ((InvoiceDataSet.InvoiceLineRow)(row)) ;
            if ( Deklarit.Data.Helpers.IsRowChanged(rowInvoiceLine) )
            {
               bool isChild ;
               if ( rowInvoiceLine.RowState != System.Data.DataRowState.Deleted )
               {
                  isChild = ( ( rowInvoiceLine.InvoiceId == rowInvoice.InvoiceId ) )  ;
               }
               else
               {
                  isChild = ( ( rowInvoiceLine["InvoiceId", System.Data.DataRowVersion.Original].Equals(rowInvoice.InvoiceId) ) )  ;
               }
               if ( isChild )
               {
                  ReadRowInvoiceline( ) ;
                  if ( rowInvoiceLine.RowState == System.Data.DataRowState.Added )
                  {
                     Gx_mode = Mode.Insert ;
                     InsertInvoiceline( ) ;
                  }
                  else
                  {
                     if ( _Gxremove )
                     {
                        Gx_mode = Mode.Delete ;
                        DeleteInvoiceline( ) ;
                     }
                     else
                     {
                        Gx_mode = Mode.Update ;
                        UpdateInvoiceline( ) ;
                     }
                  }
               }
            }
         }

         // Start of After( level) rules
         // End of After( level) rules
      }

      private void ProcessLevelInvoice( )
      {
         // Save parent mode.
         sMode2 = Gx_mode ;
         ProcessNestedLevelInvoiceline( ) ;
         // Restore parent mode.
         Gx_mode = sMode2 ;
         // ' Update level parameters
      }

      private void EndLevelInvoice( )
      {
         // Execute 'After Trn' event if defined.
         IsModified = 0 ;
      }

      private void ScanByInvoiceId( int startRow ,
                                    int maxRows )
      {
         m_WhereString = " WHERE TM1.[InvoiceId] = @InvoiceId" ;
         if ( maxRows >= 0 )
         {
            if ( startRow == 0 )
            {
               scmdbuf = "SELECT TOP " + maxRows.ToString() + "  " + m_SelectString2 + "  FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId])" + m_WhereString + " ORDER BY TM1.[InvoiceId]" ;
            }
            else
            {
               scmdbuf = " SELECT * FROM ( SELECT  " + m_SelectString2 + ", ROW_NUMBER() OVER  (  ORDER BY TM1.[InvoiceId] ) AS DK_PAGENUM   FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId]) " + m_WhereString + " ) AS DK_PAGE WHERE DK_PAGENUM BETWEEN " + (startRow + 1).ToString() + " AND " + (startRow + maxRows).ToString() ;
            }
         }
         else
         {
            scmdbuf = "SELECT " + m_SelectString2 + " FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId])" + m_WhereString + " ORDER BY TM1.[InvoiceId] " ;
         }
         cmINVOICESELECT5 = connDefault.GetCommand(scmdbuf, false) ;
         if ( cmINVOICESELECT5.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICESELECT5.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         cmINVOICESELECT5.SetParameter(0, rowInvoice["InvoiceId"]);
         INVOICESELECT5 = cmINVOICESELECT5.FetchData() ;
         RcdFound2 = 0 ;
         ScanKeyLoadInvoice( ) ;
         LoadKeyDataInvoice( maxRows) ;
         // Load Subordinate Levels
         if ( RcdFound2 > 0 )
         {
            SubLvlScanStartInvoiceline( m_WhereString, startRow, maxRows) ;
            SetParametersInvoiceIdInvoice( cmINVOICELINESELECT2) ;
            SubLvlFetchInvoiceline( ) ;
            SubLoadKeyDataInvoiceline( ) ;
         }
      }

      private void SetParametersInvoiceIdInvoice( ReadWriteCommand m_Command )
      {
         if ( m_Command.IDbCommand.Parameters.Count == 0 )
         {
            m_Command.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         m_Command.SetParameter(0, rowInvoice["InvoiceId"]);
      }

      private void ScanByClienteId( int startRow ,
                                    int maxRows )
      {
         m_WhereString = " WHERE TM1.[ClienteId] = @ClienteId" ;
         if ( maxRows >= 0 )
         {
            if ( startRow == 0 )
            {
               scmdbuf = "SELECT TOP " + maxRows.ToString() + "  " + m_SelectString2 + "  FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId])" + m_WhereString + " ORDER BY TM1.[InvoiceId]" ;
            }
            else
            {
               scmdbuf = " SELECT * FROM ( SELECT  " + m_SelectString2 + ", ROW_NUMBER() OVER  (  ORDER BY TM1.[InvoiceId] ) AS DK_PAGENUM   FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId]) " + m_WhereString + " ) AS DK_PAGE WHERE DK_PAGENUM BETWEEN " + (startRow + 1).ToString() + " AND " + (startRow + maxRows).ToString() ;
            }
         }
         else
         {
            scmdbuf = "SELECT " + m_SelectString2 + " FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId])" + m_WhereString + " ORDER BY TM1.[InvoiceId] " ;
         }
         cmINVOICESELECT5 = connDefault.GetCommand(scmdbuf, false) ;
         if ( cmINVOICESELECT5.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICESELECT5.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmINVOICESELECT5.SetParameter(0, rowInvoice["ClienteId"]);
         INVOICESELECT5 = cmINVOICESELECT5.FetchData() ;
         RcdFound2 = 0 ;
         ScanKeyLoadInvoice( ) ;
         LoadKeyDataInvoice( maxRows) ;
         // Load Subordinate Levels
         if ( RcdFound2 > 0 )
         {
            SubLvlScanStartInvoiceline( m_WhereString, startRow, maxRows) ;
            SetParametersClienteIdInvoice( cmINVOICELINESELECT2) ;
            SubLvlFetchInvoiceline( ) ;
            SubLoadKeyDataInvoiceline( ) ;
         }
      }

      private void SetParametersClienteIdInvoice( ReadWriteCommand m_Command )
      {
         if ( m_Command.IDbCommand.Parameters.Count == 0 )
         {
            m_Command.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         m_Command.SetParameter(0, rowInvoice["ClienteId"]);
      }

      private void ScanKeyStartInvoice( int startRow ,
                                        int maxRows )
      {
         m_WhereString = "" ;
         if ( maxRows >= 0 )
         {
            if ( startRow == 0 )
            {
               scmdbuf = "SELECT TOP " + maxRows.ToString() + "  " + m_SelectString2 + "  FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId])" + m_WhereString + " ORDER BY TM1.[InvoiceId]" ;
            }
            else
            {
               scmdbuf = " SELECT * FROM ( SELECT  " + m_SelectString2 + ", ROW_NUMBER() OVER  (  ORDER BY TM1.[InvoiceId] ) AS DK_PAGENUM   FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId]) " + m_WhereString + " ) AS DK_PAGE WHERE DK_PAGENUM BETWEEN " + (startRow + 1).ToString() + " AND " + (startRow + maxRows).ToString() ;
            }
         }
         else
         {
            scmdbuf = "SELECT " + m_SelectString2 + " FROM ([Invoice] TM1 WITH (NOLOCK) INNER JOIN [Cliente] T2 WITH (NOLOCK) ON T2.[ClienteId] = TM1.[ClienteId])" + m_WhereString + " ORDER BY TM1.[InvoiceId] " ;
         }
         cmINVOICESELECT5 = connDefault.GetCommand(scmdbuf, false) ;
         INVOICESELECT5 = cmINVOICESELECT5.FetchData() ;
         RcdFound2 = 0 ;
         ScanKeyLoadInvoice( ) ;
         LoadKeyDataInvoice( maxRows) ;
         // Load Subordinate Levels
         if ( RcdFound2 > 0 )
         {
            SubLvlScanStartInvoiceline( m_WhereString, startRow, maxRows) ;
            SetParametersStartInvoiceInvoice( cmINVOICELINESELECT2) ;
            SubLvlFetchInvoiceline( ) ;
            SubLoadKeyDataInvoiceline( ) ;
         }
      }

      private void SetParametersStartInvoiceInvoice( ReadWriteCommand m_Command )
      {
      }

      private void ScanKeyNextInvoice( )
      {
         cmINVOICESELECT5.HasMoreRows = INVOICESELECT5.Read() ;
         RcdFound2 = 0 ;
         ScanKeyLoadInvoice( ) ;
      }

      private void ScanKeyLoadInvoice( )
      {
         sMode2 = Gx_mode ;
         Gx_mode = Mode.Display ;
         if ( cmINVOICESELECT5.HasMoreRows )
         {
            RcdFound2 = 1 ;
            rowInvoice["InvoiceId"] = dsDefault.Db.GetInt16(INVOICESELECT5, 0) ;
            rowInvoice["InvoiceDescription"] = dsDefault.Db.GetString(INVOICESELECT5, 1) ;
            rowInvoice["ClienteName"] = dsDefault.Db.GetString(INVOICESELECT5, 2) ;
            rowInvoice["InvoiceLineLastIndex"] = dsDefault.Db.GetInt16(INVOICESELECT5, 3) ;
            rowInvoice["ClienteId"] = dsDefault.Db.GetInt16(INVOICESELECT5, 4) ;
            rowInvoice["ClienteName"] = dsDefault.Db.GetString(INVOICESELECT5, 2) ;
         }
         Gx_mode = sMode2 ;
      }

      private void ScanKeyEndInvoice( )
      {
         INVOICESELECT5.Close();
      }

      private void AfterConfirmInvoice( )
      {
         // After Confirm Rules
         OnInvoiceUpdating( new InvoiceEventArgs( rowInvoice, Gx_mode)) ;
      }

      private void CreateNewRowInvoice( )
      {
         rowInvoice = InvoiceSet.Invoice.NewInvoiceRow() ;
      }

      private void LoadChildInvoice( int startRow ,
                                     int maxRows )
      {
         CreateNewRowInvoice( ) ;
         bool tmpConstraintState = InvoiceSet.EnforceConstraints ;
         InvoiceSet.InvoiceLine.BeginLoadData( ) ;
         InvoiceSet.Invoice.BeginLoadData( ) ;
         ScanKeyStartInvoice( startRow, maxRows) ;
         InvoiceSet.InvoiceLine.EndLoadData( ) ;
         InvoiceSet.Invoice.EndLoadData( ) ;
         this.InvoiceSet.EnforceConstraints = tmpConstraintState ;
      }

      private void LoadKeyDataInvoice( int maxRows )
      {
         int loadedRows = 0 ;
         if ( RcdFound2 != 0 )
         {
            ScanKeyLoadInvoice( ) ;
            while ( ( RcdFound2 != 0 ) && ( loadedRows != maxRows ) )
            {
               loadedRows++ ;
               LoadRowInvoice( ) ;
               CreateNewRowInvoice( ) ;
               ScanKeyNextInvoice( ) ;
            }
         }
         if ( loadedRows > 0 )
         {
            RcdFound2 = 1 ;
         }
         ScanKeyEndInvoice( ) ;
         if ( InvoiceSet.Invoice.Count > 0 )
         {
            rowInvoice = InvoiceSet.Invoice[InvoiceSet.Invoice.Count -1] ;
         }
      }

      private void LoadByInvoiceId( int startRow ,
                                    int maxRows )
      {
         bool tmpConstraintState = InvoiceSet.EnforceConstraints ;
         InvoiceSet.InvoiceLine.BeginLoadData( ) ;
         InvoiceSet.Invoice.BeginLoadData( ) ;
         ScanByInvoiceId( startRow, maxRows) ;
         InvoiceSet.InvoiceLine.EndLoadData( ) ;
         InvoiceSet.Invoice.EndLoadData( ) ;
         this.InvoiceSet.EnforceConstraints = tmpConstraintState ;
         if ( InvoiceSet.Invoice.Count > 0 )
         {
            rowInvoice = InvoiceSet.Invoice[InvoiceSet.Invoice.Count -1] ;
         }
      }

      private void LoadByClienteId( int startRow ,
                                    int maxRows )
      {
         bool tmpConstraintState = InvoiceSet.EnforceConstraints ;
         InvoiceSet.InvoiceLine.BeginLoadData( ) ;
         InvoiceSet.Invoice.BeginLoadData( ) ;
         ScanByClienteId( startRow, maxRows) ;
         InvoiceSet.InvoiceLine.EndLoadData( ) ;
         InvoiceSet.Invoice.EndLoadData( ) ;
         this.InvoiceSet.EnforceConstraints = tmpConstraintState ;
         if ( InvoiceSet.Invoice.Count > 0 )
         {
            rowInvoice = InvoiceSet.Invoice[InvoiceSet.Invoice.Count -1] ;
         }
      }

      private void CreateNewRowInvoiceline( )
      {
         rowInvoiceLine = InvoiceSet.InvoiceLine.NewInvoiceLineRow() ;
      }

      private void LoadChildInvoiceline( )
      {
         CreateNewRowInvoiceline( ) ;
         ScanKeyStartInvoiceline( ) ;
      }

      private void LoadKeyDataInvoiceline( )
      {
         while ( RcdFound3 != 0 )
         {
            LoadRowInvoiceline( ) ;
            CreateNewRowInvoiceline( ) ;
            ScanKeyNextInvoiceline( ) ;
         }
         ScanKeyEndInvoiceline( ) ;
      }

      private void SubLoadKeyDataInvoiceline( )
      {
         while ( RcdFound3 != 0 )
         {
            LoadRowInvoiceline( ) ;
            CreateNewRowInvoiceline( ) ;
            ScanKeyNextInvoiceline( ) ;
         }
         ScanKeyEndInvoiceline( ) ;
      }

      private void AddRowInvoice( )
      {
         InvoiceSet.Invoice.AddInvoiceRow( rowInvoice) ;
      }

      private readonly string m_SelectString2 = "TM1.[InvoiceId], TM1.[InvoiceDescription], T2.[ClienteName], TM1.[InvoiceLineLastIndex], TM1.[ClienteId]" ;


      public delegate  void InvoiceLineUpdateEventHandler( object sender ,
                                                           InvoiceLineEventArgs e );
      public event InvoiceLineUpdateEventHandler InvoiceLineUpdated ;
      private void OnInvoiceLineUpdated( InvoiceLineEventArgs e )
      {
         if ( this.InvoiceLineUpdated != null )
         {
            this.InvoiceLineUpdated( this, e) ;
         }
      }

      public event InvoiceLineUpdateEventHandler InvoiceLineUpdating ;
      private void OnInvoiceLineUpdating( InvoiceLineEventArgs e )
      {
         if ( this.InvoiceLineUpdating != null )
         {
            this.InvoiceLineUpdating( this, e) ;
         }
      }

      public class InvoiceLineEventArgs : EventArgs
      {
         private System.Data.StatementType m_statementType ;
         private InvoiceDataSet.InvoiceLineRow m_dataRow ;
         public InvoiceLineEventArgs( InvoiceDataSet.InvoiceLineRow row ,
                                      StatementType statementType )
         {
            m_dataRow = row ;
            m_statementType = statementType ;
         }

         public System.Data.StatementType StatementType
         {
            get {
               return m_statementType ;
            }

         }

         public InvoiceDataSet.InvoiceLineRow Row
         {
            get {
               return m_dataRow ;
            }

            set {
               m_dataRow = value ;
            }

         }

      }


      public delegate  void InvoiceUpdateEventHandler( object sender ,
                                                       InvoiceEventArgs e );
      public event InvoiceUpdateEventHandler InvoiceUpdated ;
      private void OnInvoiceUpdated( InvoiceEventArgs e )
      {
         if ( this.InvoiceUpdated != null )
         {
            this.InvoiceUpdated( this, e) ;
         }
      }

      public event InvoiceUpdateEventHandler InvoiceUpdating ;
      private void OnInvoiceUpdating( InvoiceEventArgs e )
      {
         if ( this.InvoiceUpdating != null )
         {
            this.InvoiceUpdating( this, e) ;
         }
      }

      public class InvoiceEventArgs : EventArgs
      {
         private System.Data.StatementType m_statementType ;
         private InvoiceDataSet.InvoiceRow m_dataRow ;
         public InvoiceEventArgs( InvoiceDataSet.InvoiceRow row ,
                                  StatementType statementType )
         {
            m_dataRow = row ;
            m_statementType = statementType ;
         }

         public System.Data.StatementType StatementType
         {
            get {
               return m_statementType ;
            }

         }

         public InvoiceDataSet.InvoiceRow Row
         {
            get {
               return m_dataRow ;
            }

            set {
               m_dataRow = value ;
            }

         }

      }


      // Custom Exceptions
      [Serializable()]
      public class InvoiceNotFoundException : Deklarit.DataNotFoundException
      {
         public InvoiceNotFoundException( )
         {
         }

         public InvoiceNotFoundException( string message ) : base(message)
         {
         }

         public InvoiceNotFoundException( string message ,
                                          Exception inner ) : base(message, inner)
         {
         }

         protected InvoiceNotFoundException( SerializationInfo info ,
                                             StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class ProductForeignKeyNotFoundException : Deklarit.ForeignKeyNotFoundException
      {
         public ProductForeignKeyNotFoundException( )
         {
         }

         public ProductForeignKeyNotFoundException( string message ) : base(message)
         {
         }

         public ProductForeignKeyNotFoundException( string message ,
                                                    Exception inner ) : base(message, inner)
         {
         }

         protected ProductForeignKeyNotFoundException( SerializationInfo info ,
                                                       StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class InvoiceLineDataLockedException : Deklarit.DataLockedException
      {
         public InvoiceLineDataLockedException( )
         {
         }

         public InvoiceLineDataLockedException( string message ) : base(message)
         {
         }

         public InvoiceLineDataLockedException( string message ,
                                                Exception inner ) : base(message, inner)
         {
         }

         protected InvoiceLineDataLockedException( SerializationInfo info ,
                                                   StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class InvoiceLineDataChangedException : Deklarit.DataChangedException
      {
         public InvoiceLineDataChangedException( )
         {
         }

         public InvoiceLineDataChangedException( string message ) : base(message)
         {
         }

         public InvoiceLineDataChangedException( string message ,
                                                 Exception inner ) : base(message, inner)
         {
         }

         protected InvoiceLineDataChangedException( SerializationInfo info ,
                                                    StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class InvoiceLineDuplicateKeyException : Deklarit.DuplicateKeyException
      {
         public InvoiceLineDuplicateKeyException( )
         {
         }

         public InvoiceLineDuplicateKeyException( string message ) : base(message)
         {
         }

         public InvoiceLineDuplicateKeyException( string message ,
                                                  Exception inner ) : base(message, inner)
         {
         }

         protected InvoiceLineDuplicateKeyException( SerializationInfo info ,
                                                     StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class ClienteForeignKeyNotFoundException : Deklarit.ForeignKeyNotFoundException
      {
         public ClienteForeignKeyNotFoundException( )
         {
         }

         public ClienteForeignKeyNotFoundException( string message ) : base(message)
         {
         }

         public ClienteForeignKeyNotFoundException( string message ,
                                                    Exception inner ) : base(message, inner)
         {
         }

         protected ClienteForeignKeyNotFoundException( SerializationInfo info ,
                                                       StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class InvoiceDataLockedException : Deklarit.DataLockedException
      {
         public InvoiceDataLockedException( )
         {
         }

         public InvoiceDataLockedException( string message ) : base(message)
         {
         }

         public InvoiceDataLockedException( string message ,
                                            Exception inner ) : base(message, inner)
         {
         }

         protected InvoiceDataLockedException( SerializationInfo info ,
                                               StreamingContext context ) : base(info, context)
         {
         }

      }

      [Serializable()]
      public class InvoiceDataChangedException : Deklarit.DataChangedException
      {
         public InvoiceDataChangedException( )
         {
         }

         public InvoiceDataChangedException( string message ) : base(message)
         {
         }

         public InvoiceDataChangedException( string message ,
                                             Exception inner ) : base(message, inner)
         {
         }

         protected InvoiceDataChangedException( SerializationInfo info ,
                                                StreamingContext context ) : base(info, context)
         {
         }

      }


      protected void Cleanup( )
      {
         dsDefault.Dispose();
      }


      private void Initialize( )
      {
         resourceManager = Deklarit.Utils.ResourceManager.Instance ;
         resourceManagerTables = new System.Resources.ResourceManager( "Deklarit.Tables", System.Reflection.Assembly.GetExecutingAssembly()) ;
         scmdbuf = "" ;
         m__InvoiceLineProductValueOriginal = new object();
         m__InvoiceLineProductQuantityOriginal = new object();
         m__ProductIdOriginal = new object();
         m_SubSelTopString3 = "" ;
         m_WhereStringSub = "" ;
         m__InvoiceDescriptionOriginal = new object();
         m__InvoiceLineLastIndexOriginal = new object();
         m__ClienteIdOriginal = new object();
         m_WhereString = "" ;
         if ( this.Transaction == null )
         {
            this.Transaction = ((System.Data.IDbTransaction)(System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot(DeklaritTransaction.TransactionSlotName)) ));
         }
      }

      private short RcdFound2 ;
      private short RcdFound3 ;
      private short IsModified ;
      private int recordCount ;
      private String scmdbuf ;
      private String m_SubSelTopString3 ;
      private String m_WhereStringSub ;
      private String m_WhereString ;
      private bool _Gxremove ;
      private DataStore dsDefault ;
      private System.Resources.ResourceManager resourceManager ;
      private System.Resources.ResourceManager resourceManagerTables ;
      private ReadWriteConnection connDefault ;
      private ReadWriteCommand cmINVOICESELECT1 ;
      private IDataReader INVOICESELECT1 ;
      private ReadWriteCommand cmINVOICESELECT2 ;
      private IDataReader INVOICESELECT2 ;
      private object m__InvoiceLineProductValueOriginal ;
      private object m__InvoiceLineProductQuantityOriginal ;
      private object m__ProductIdOriginal ;
      private IDataReader INVOICELINESELECT1 ;
      private ReadWriteCommand cmINVOICELINESELECT2 ;
      private IDataReader INVOICELINESELECT2 ;
      private object m__InvoiceDescriptionOriginal ;
      private object m__InvoiceLineLastIndexOriginal ;
      private object m__ClienteIdOriginal ;
      private IDataReader INVOICESELECT4 ;
      private ReadWriteCommand cmINVOICESELECT5 ;
      private IDataReader INVOICESELECT5 ;
      private System.Data.StatementType Gx_mode ;
      private System.Data.StatementType sMode3 ;
      private System.Data.StatementType sMode2 ;
      private ReadWriteCommand m_Command ;
   }

   public class InvoiceDataReader : System.IDisposable
   {
      public InvoiceDataReader( )
      {
      }

      private void init_reader( )
      {
         this.Initialize( ) ;
         dsDefault = new DataStore( new SqlServer2005Handler( ), "System.Data.SqlClient", DeklaritModel.Configuration.ConnectionString, DeklaritTransaction.TransactionSlotName) ;
         m_Disposed = false ;
         m_Closed = true ;
      }

      private IDbTransaction daCurrentTransaction ;
      ///  <summary>
      ///   Gets or sets the Transaction object to be used  by  InvoiceDataReaderDeklarit DataReaderclasses
      ///    to commit changes to the Database.
      ///  </summary>
      public IDbTransaction Transaction
      {
         get {
            return daCurrentTransaction ;
         }

         set {
            daCurrentTransaction = value ;
         }

      }

      public IDataReader Open( )
      {
         init_reader( ) ;
         m_Closed = false ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         cmINVOICESELECT6 = connDefault.GetCommand("SELECT TM1.[InvoiceId], TM1.[InvoiceDescription], T2.[ClienteName], TM1.[InvoiceLineLastIndex], TM1.[ClienteId] FROM ([Invoice] TM1 LEFT JOIN [Cliente] T2 ON T2.[ClienteId] = TM1.[ClienteId]) ORDER BY TM1.[InvoiceId] ", false) ;
         INVOICESELECT6 = cmINVOICESELECT6.ExecuteReader(((daCurrentTransaction==null) ? Configuration.ReaderCommandBehavior : CommandBehavior.Default)) ;
         return INVOICESELECT6 ;
      }

      public IDataReader OpenByInvoiceId( short invoiceId )
      {
         init_reader( ) ;
         m_Closed = false ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         cmINVOICESELECT6 = connDefault.GetCommand("SELECT TM1.[InvoiceId], TM1.[InvoiceDescription], T2.[ClienteName], TM1.[InvoiceLineLastIndex], TM1.[ClienteId] FROM ([Invoice] TM1 LEFT JOIN [Cliente] T2 ON T2.[ClienteId] = TM1.[ClienteId]) WHERE TM1.[InvoiceId] = @InvoiceId ORDER BY TM1.[InvoiceId] ", false) ;
         if ( cmINVOICESELECT6.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICESELECT6.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@InvoiceId", System.Data.DbType.Int16));
         }
         cmINVOICESELECT6.SetParameter(0, invoiceId);
         INVOICESELECT6 = cmINVOICESELECT6.ExecuteReader(((daCurrentTransaction==null) ? Configuration.ReaderCommandBehavior : CommandBehavior.Default)) ;
         return INVOICESELECT6 ;
      }

      public IDataReader OpenByClienteId( short clienteId )
      {
         init_reader( ) ;
         m_Closed = false ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         cmINVOICESELECT6 = connDefault.GetCommand("SELECT TM1.[InvoiceId], TM1.[InvoiceDescription], T2.[ClienteName], TM1.[InvoiceLineLastIndex], TM1.[ClienteId] FROM ([Invoice] TM1 LEFT JOIN [Cliente] T2 ON T2.[ClienteId] = TM1.[ClienteId]) WHERE TM1.[ClienteId] = @ClienteId ORDER BY TM1.[InvoiceId] ", false) ;
         if ( cmINVOICESELECT6.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICESELECT6.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         cmINVOICESELECT6.SetParameter(0, clienteId);
         INVOICESELECT6 = cmINVOICESELECT6.ExecuteReader(((daCurrentTransaction==null) ? Configuration.ReaderCommandBehavior : CommandBehavior.Default)) ;
         return INVOICESELECT6 ;
      }

      public int DeleteByClienteId( short clienteId )
      {
         init_reader( ) ;
         int intCount = 0 ;
         connDefault = dsDefault.GetReadWriteConnection( daCurrentTransaction) ;
         cmINVOICEDELETE2 = connDefault.GetCommand("DELETE FROM [Invoice]  WHERE [ClienteId] = @ClienteId", false) ;
         if ( cmINVOICEDELETE2.IDbCommand.Parameters.Count == 0 )
         {
            cmINVOICEDELETE2.IDbCommand.Parameters.Add(  dsDefault.GetDbParameter( "@ClienteId", System.Data.DbType.Int16));
         }
         // Using cursor INVOICEDELETE2
         cmINVOICEDELETE2.SetParameter(0, clienteId);
         intCount = cmINVOICEDELETE2.ExecuteStmt() ;
         return intCount ;
      }

      public void Dispose( )
      {
         if ( ! m_Disposed )
         {
            m_Disposed = true ;
            try
            {
               if ( ! m_Closed && ( INVOICESELECT6 != null ) )
               {
                  m_Closed = true ;
                  INVOICESELECT6.Close();
               }
            }
            finally
            {
               try
               {
                  connDefault.Close();
               }
               finally
               {
                  this.Cleanup();
               }
            }
         }
      }

      public void Close( )
      {
         this.Dispose( ) ;
      }


      protected void Cleanup( )
      {
         dsDefault.Dispose();
      }


      private void Initialize( )
      {
         scmdbuf = "" ;
         if ( this.Transaction == null )
         {
            this.Transaction = ((System.Data.IDbTransaction)(System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot(DeklaritTransaction.TransactionSlotName)) ));
         }
      }

      private String scmdbuf ;
      private bool m_Disposed ;
      private bool m_Closed ;
      private DataStore dsDefault ;
      private ReadWriteConnection connDefault ;
      private ReadWriteCommand cmINVOICESELECT6 ;
      private IDataReader INVOICESELECT6 ;
      private ReadWriteCommand cmINVOICEDELETE2 ;
   }

}
